package com.yzt.dynamicmodule.activity;

import static com.yzt.zhuju.arouter.RouterDynamic.ACTIVITY_DYNAMIC_TEXT_ADD;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.user.RefreshDynamicEvent;
import com.yzt.zhuju.mvp.MVPActivity;
import com.yzt.zhuju.widget.tuiview.TUITextView;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.contacts.AddDynamicContacts;
import com.yzt.dynamicmodule.presenter.AddDynamicPresenter;
import com.yzt.p_base.utils.ToastUtil;

/**
 * author : bob
 * date   : 2023/2/27
 * desc   : 新增文本动态
 */
@SuppressLint("SetTextI18n")
@Route(path = ACTIVITY_DYNAMIC_TEXT_ADD)
public class DynamicAddTextActivity extends MVPActivity<AddDynamicPresenter> implements AddDynamicContacts.View {

    private EditText tvTitle;

    private TUITextView tv1, tv2, tv3, tv4;

    private RelativeLayout rl_container;

    private boolean bigSize = false;

    private String color = "#000000";

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_dynamic_text;
    }

    @Override
    public void installViews() {
        setFullScreen(true);

        initView();
    }

    private void initView() {
        ImageView tvCancel = findViewById(R.id.tv_cancel);
        tvCancel.setOnClickListener(this);
        TUITextView tvPub = findViewById(R.id.tv_pub);
        tvPub.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        rl_container = findViewById(R.id.rl_container);
        tv1 = findViewById(R.id.tv_color_1);
        tv2 = findViewById(R.id.tv_color_2);
        tv3 = findViewById(R.id.tv_color_3);
        tv4 = findViewById(R.id.tv_color_4);

        color = "#000000";
        rl_container.setBackgroundColor(Color.parseColor("#000000"));
        tvTitle.setTextColor(Color.parseColor("#FFFFFF"));
        tv1.setSelected(true);
        tv2.setSelected(false);
        tv3.setSelected(false);
        tv4.setSelected(false);

        tv1.setOnClickListener(v -> {
            color = "#000000";
            rl_container.setBackgroundColor(Color.parseColor("#000000"));
            tvTitle.setTextColor(Color.parseColor("#FFFFFF"));
            tv1.setSelected(true);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
        });
        tv2.setOnClickListener(v -> {
            color = "#FFF5E8";
            rl_container.setBackgroundColor(Color.parseColor("#FFF5E8"));
            tvTitle.setTextColor(Color.parseColor("#000000"));
            tv1.setSelected(false);
            tv2.setSelected(true);
            tv3.setSelected(false);
            tv4.setSelected(false);
        });
        tv3.setOnClickListener(v -> {
            color = "#E1C4EF";
            rl_container.setBackgroundColor(Color.parseColor("#E1C4EF"));
            tvTitle.setTextColor(Color.parseColor("#000000"));
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(true);
            tv4.setSelected(false);
        });
        tv4.setOnClickListener(v -> {
            color = "#B5C5E5";
            rl_container.setBackgroundColor(Color.parseColor("#B5C5E5"));
            tvTitle.setTextColor(Color.parseColor("#000000"));
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(true);
        });
        findViewById(R.id.img_content_size).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bigSize) {
                    bigSize = false;
                    tvTitle.setTextSize(getResources().getDimension(R.dimen.dp_16));
                } else {
                    bigSize = true;
                    tvTitle.setTextSize(getResources().getDimension(R.dimen.dp_24));
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.tv_cancel) {
            // 取消
            finish();
        } else if (view.getId() == R.id.tv_pub) {
            // 发布
            addNewDynamic();
        }
    }

    /**
     * 新增动态
     */
    private void addNewDynamic() {

        String content = tvTitle.getText().toString();

        if (content.equals("")) {
            ToastUtil.showMessage("请输入内容～");
            return;
        }

        showMProgress();
        JSONObject json = new JSONObject();
        // 活动附件类型（图片，视频 [ 1：图片，2：视频 ]
        json.put("bgColor", color);
        json.put("content", content);
        // 动态类型 图文，视频，纯文本 [ 1：图片，2：视频，3：纯文本 ]
        json.put("contentType", 3);
        mPresenter.publicDynamic(json);
    }

    @Override
    public void publishSuccess() {
        ToastUtil.showMessage(getString(R.string.dynamic_publish_success));
        RxBus.getDefault().post(new RefreshDynamicEvent());
        finish();
    }

    @Override
    public void publishEnd() {
        dismissLoadingDialog();
    }
}